package top.blackr.folivora.common.security.auth.authInfo;

import cn.dev33.satoken.stp.StpUtil;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.redis.cache.MicaRedisCache;
import top.blackr.folivora.common.core.constants.RedisConstants;
import top.blackr.folivora.common.security.auth.SecUser;
import top.blackr.folivora.common.security.auth.SecurityUtils;

/**
 * @author Administrator
 */
@AllArgsConstructor
public class DefaultAuthenticationInfo implements AuthenticationInfo {

    private final MicaRedisCache redisCache;

    @Override
    public SecUser login(SecUser secUser) {
        StpUtil.login(secUser.getUserId());
        // 设置token
        secUser.setTokenInfo(StpUtil.getTokenInfo());
        // 返回用户拥有的角色&权限
        secUser.setRoles(StpUtil.getRoleList());
        secUser.setPermissions(StpUtil.getPermissionList());
        SecurityUtils.setUser(secUser);
        redisCache.hSet(RedisConstants.User.USER_INFO_ID, secUser.getUserId(), secUser);
        return secUser;
    }
}
